CREATE OR REPLACE FUNCTION fill_up_clustres_info(data_table_name text, attribute_name text, data_id_name text, geom_col_name text, clusters_table_name text, clusters_feat_id text, level_col_name text)
RETURNS void AS
$BODY$
DECLARE
  levels integer;
  max_bbox_geom geometry;
  max_bbox_area_value double precision;
  postgis_version text;
  
BEGIN
	SELECT postgis_lib_version() INTO postgis_version;
	EXECUTE 'SELECT COUNT(DISTINCT('|| quote_ident(level_col_name) ||')) FROM '|| clusters_table_name::regclass INTO levels;
	EXECUTE 'SELECT ST_Envelope(ST_Collect('|| quote_ident(geom_col_name) ||')) FROM '|| data_table_name::regclass INTO max_bbox_geom; 
	
	IF (postgis_version LIKE '1.3._') OR (postgis_version LIKE '1.4._') THEN
		SELECT ST_Area(ST_Transform(max_bbox_geom, 3035)) INTO max_bbox_area_value;
	ELSE
		SELECT ST_Area(Geography(max_bbox_geom)) INTO max_bbox_area_value;
	END IF;
	
	INSERT INTO clusters_info(data_table, data_attribute, data_id, clusters_table, clusters_feature_id, clusters_level_column, levels_count, max_bbox, max_bbox_area) 
	   VALUES(data_table_name, attribute_name, data_id_name, clusters_table_name, clusters_feat_id, level_col_name, levels, max_bbox_geom, max_bbox_area_value);
END;
$BODY$
LANGUAGE plpgsql;